<template>
  <div class="resource-list">
    <el-card>
      <div slot="header" class="clearfix">
        <el-form ref="form" :model="from" :inline="true">
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="from.name" placeholder="资源名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="from.url" placeholder="资源路径" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="cateoryId">
            <el-select v-model="from.cateoryId" placeholder="资源分类" clearable>
              <el-option v-for="item in resourcesCategories" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <!-- <el-input v-model="from.cateoryId" placeholder="资源分类"></el-input> -->
          </el-form-item>
          <el-form-item>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button>添加角色</el-button>
      <el-table
        border
        :data="resources"
        style="width: 100%"
        v-loading='isLoading'
      >
        <el-table-column
          type="index"
          label="编号"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="资源名称"
        ></el-table-column>
        <el-table-column
          prop="url"
          label="资源路径"
        ></el-table-column>
        <el-table-column
          prop="description"
          label="描述"
        ></el-table-column>
        <el-table-column
          label="添加时间">
          <template slot-scope="scope">
            <span>{{scope.row.createdTime | dataFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
              <el-button
                type="text"
              >分配菜单</el-button>
              <el-button
                type="text"
              >分配资源</el-button>
            </div>
            <div>
              <el-button
                size="mini"
                @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button
                size="mini"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="from.current"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :disabled="isLoading"
        >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getResource, getResourceCategories } from '@/services/resource'
export default {
  name: 'resource-list',
  data () {
    return {
      from: {
        current: 2,
        size: 10,
        name: '',
        url: '',
        cateoryId: ''
      },
      // 用于保存加载状态
      isLoading: false,
      resources: [],
      total: 100,
      // 存储资源分类信息
      resourcesCategories: []
    }
  },
  created () {
    this.loadResources()
    this.loadResourcesCategories()
  },
  methods: {
    // 加载资源列表
    async loadResourcesCategories () {
      const { data } = await getResourceCategories()
      // console.log(data)
      if (data.code === '000000') {
        this.resourcesCategories = data.data
      }
    },
    async loadResources () {
      this.isLoading = true
      const { data } = await getResource(this.from)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.total = data.data.total
        this.isLoading = false
      }
      // console.log(data)
    },
    handleSizeChange (oldvalue) {
      this.from.size = oldvalue
      this.from.current = 1
      this.loadResources()
    },
    handleCurrentChange (oldvalue) {
      this.loadResources()
      this.from.current = oldvalue
    },
    handleEdit () {},
    handleDelete () {},
    onSubmit () {
      this.from.current = 1
      this.loadResources()
    },
    onReset () {
      this.$refs.form.resetFields()
    }
  },
  filters: {
    dataFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
